package com.wontube.smr.cron;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wontube.core.cron.IJobs;
import com.wontube.smr.bo.sites.youtube.spider.YoutubeMovieSpider;
import com.wontube.smr.bo.sites.youtube.spider.YoutubeMusicSpider;



/** 
 * @author tanggf(2012-4-6)
 *
 */
public class ObtainYoutubeJobs implements IJobs {
	private final static Logger logger = LoggerFactory.getLogger(ObtainYoutubeJobs.class);

	@Override
	public String name() {
		return "obtain-youtube";
	}

	//可能参数-daily_chart, -weekly_chart, -movies
	@Override
	public IJobs execute(String[] args) {
		//防止参数重复
		Set<String> set = new HashSet<String>();
		for (String arg : args) {
			set.add(arg);
		}
		
		if(set.contains("-daily_chart")){
			obtainDailyChart();
		}
		
		if(set.contains("-weekly_chart")){
			obtainWeeklyChart();
		}
		
		if(set.contains("-movies")){
			this.obtainMovies();
		}
		
		if(set.contains("-musics")){
			this.obtainMusics();
		}
		
		return this;
	}

	private void obtainWeeklyChart() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = YoutubeMusicSpider.grabWeeklyMusicCharts();
		} catch (Exception e) {
			logger.error("error happen when obtail youtube weekly music chart", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} weekly chart items, cost {} s", count, cost);
	}

	private void obtainDailyChart() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = YoutubeMusicSpider.grabDailyMusicCharts();
		} catch (Exception e) {
			logger.error("error happen when obtail youtube daily music chart", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} daily chart items, cost {} s", count, cost);
	}
	
	private void obtainMovies(){
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = YoutubeMovieSpider.grabAllMovies();
		} catch (Exception e) {
			logger.error("error happen when grab youtube movies", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Youtube movies, cost {} s", count, cost);
	}
	
	private void obtainMusics() {
		long start = System.currentTimeMillis();
		int count = 0;
		try {
			count = YoutubeMusicSpider.grabAllMusics();
		} catch (Exception e) {
			logger.error("error happen when grab youtube musics", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Youtube musics, cost {} s", count, cost);
		
	}
}
